29 January 2020, 09:00-11:30
R Homepage - LINK
The Comprehensive R Archive Network (CRAN) - LINK
Turkey - Middle East Technical University Northern Cyprus Campus, Mersin - LINK
An Integrated Development Environment (IDE) for R.
File - New Project
Install, Update
25 * 4 + 9 / 3 - 56
The assignment arrow (<-)
Numeric (Double)
Integer
Complex
Logical
Character
Special Values
Date/Time
Arithmetic Operators ( +, -, /, x )
Logical Operators ( <, >, ==, != ….)
Import Dataset -> From Text (readr) -> Browse
Console
read.table()
read.delim()
read.csv()
write.table()
Uptade Preview
If your situation has more than two mutually exclusive cases, use else and if statements together.
lower < 7 < upper
X >= 2 & X <= 7
(X[lower] - 1)/36
X > 7 & X <= 12
13 - X[upper])/36
w <- 1
x <- 2
y <- 3
z <- 4
my_fun1 <- function(){
result1 <- w+x
result2 <- y*z
print(result1)
print(result2)
}
my_fun1()## [1] 3
## [1] 12
my_fun3 <- function(w,x,y,z){
result1 <- w+x
result2 <- y*z
print(result1)
print(result2)
}
# my_fun3()
my_fun3(1,2,3,4)## [1] 3
## [1] 12
menu(c("Yes", "No"), title="What dou you think?")
menu1 <- menu(c("Yes", "No"), title="What dou you think?")
menu1
menu(c("Option1","Option2","Option3","Option4"), title="Choose one of them")
menu2 <- menu(c("Option1","Option2","Option3","Option4"), title="Choose one of them")
menu2
my_fun3 <- function(x,y){
math <- menu(c("+", "-", "*", "/"), title="Which calculation?")
if (math==1) {
result <- x+y
} else if ( ) {
} else if ( ) {
} else {
}
print(result)
}
if ( condition ) {
print( )
}
worry_flow <- function() {
answer1 <- menu()
if () {
print()
}
else {
answer2 <- menu()
}
}
worry_flow <- function() {
answer1 <- menu()
if () {
print()
}
else {
answer2 <- menu()
if(){
print()
}
else{
}
}
}
worry_flow <- function() {
answer1 <- menu(c("Yes","No"), title = "Do you have a problem in your life ?")
if (answer1 == 2) {
print("Then Don't Worry")
}
else {
answer2 <- menu(c("Yes","No"), title = "Can you do something about it ?")
if(answer2 == 1){
print("Then Don't Worry")
}
else{
print("Then Don't Worry")
}
}
}worry_flow <- function() {
answer1 <- menu(c("Yes","No"), title = "Do you have a problem in your life ?")
if (answer1 == 2) {
print("Then Don't Worry")
}
else {
answer2 <- menu(c("Yes","No"), title = "Can you do something about it ?")
if(answer2 == 1){
print("Then Don't Worry")
}
else{
print("Then Don't Worry")
}
}
require(tcltk)
msgBox <- tkmessageBox(title = "Title of message box",
message = "THEN WYH WORRY!",
icon = "info",
type = "ok")
}CRU_TR_Near-Surface_Temp_16-01-1901_16-12-2012_Monthly
CRU_TR_Near-Surface_Temp_16-01-1901_16-12-2012_Monthly
install.packages("ncdf4")
install.packages("RNetCDF")
install.packages("maptools")
install.packages("fields")
## File cru_1901_2012_tmp_TR.nc (NC_FORMAT_CLASSIC):
##
## 1 variables (excluding dimension variables):
## double tmp[lon,lat,time]
## long_name: near-surface temperature
## units: degrees Celsius
## _FillValue: 9.96920996838687e+36
## missing_value: 9.96920996838687e+36
## correlation_decay_distance: 1200
##
## 3 dimensions:
## lon Size:42
## standard_name: longitude
## long_name: longitude
## units: degrees_east
## axis: X
## lat Size:18
## standard_name: latitude
## long_name: latitude
## units: degrees_north
## axis: Y
## time Size:1344 *** is unlimited ***
## standard_name: time
## long_name: time
## units: days since 1900-01-01 00:00:00
## calendar: standard
##
## 10 global attributes:
## CDI: Climate Data Interface version 1.6.9 (http://mpimet.mpg.de/cdi)
## Conventions: CF-1.4
## history: Mon Feb 22 16:24:47 2016: cdo sellonlatbox,25,46,34,43 cru_ts3.21.1901.2012.tmp.dat.nc cru_1901_2012_tmp_TR.nc
## Wed 10 Jul 2013 17:04:15 BST : User ianharris : Program makegridsauto.for called by update.for
## source: Run ID = 1307101324
## Data generated by BADC from:
## tmp.1307101049.dtb
## institution: Data held at British Atmospheric Data Centre, RAL, UK.
## title: CRU TS3.21 Mean Temperature
## references: Information on the data is available at http://badc.nerc.ac.uk/data/cru/
## comment: Data restrictions: for academic research use only.Contact BADC for details
## contact: BADC <badc@rl.ac.uk>
## CDO: Climate Data Operators version 1.7.0rc2 (http://mpimet.mpg.de/cdo)
## netcdf classic {
## dimensions:
## lon = 42 ;
## lat = 18 ;
## time = UNLIMITED ; // (1344 currently)
## variables:
## NC_DOUBLE lon(lon) ;
## NC_CHAR lon:standard_name = "longitude" ;
## NC_CHAR lon:long_name = "longitude" ;
## NC_CHAR lon:units = "degrees_east" ;
## NC_CHAR lon:axis = "X" ;
## NC_DOUBLE lat(lat) ;
## NC_CHAR lat:standard_name = "latitude" ;
## NC_CHAR lat:long_name = "latitude" ;
## NC_CHAR lat:units = "degrees_north" ;
## NC_CHAR lat:axis = "Y" ;
## NC_DOUBLE time(time) ;
## NC_CHAR time:standard_name = "time" ;
## NC_CHAR time:long_name = "time" ;
## NC_CHAR time:units = "days since 1900-01-01 00:00:00" ;
## NC_CHAR time:calendar = "standard" ;
## NC_DOUBLE tmp(lon, lat, time) ;
## NC_CHAR tmp:long_name = "near-surface temperature" ;
## NC_CHAR tmp:units = "degrees Celsius" ;
## NC_DOUBLE tmp:_FillValue = 9.96920996838687e+36 ;
## NC_DOUBLE tmp:missing_value = 9.96920996838687e+36 ;
## NC_DOUBLE tmp:correlation_decay_distance = 1200 ;
##
## // global attributes:
## NC_CHAR :CDI = "Climate Data Interface version 1.6.9 (http://mpimet.mpg.de/cdi)" ;
## NC_CHAR :Conventions = "CF-1.4" ;
## NC_CHAR :history = "Mon Feb 22 16:24:47 2016: cdo sellonlatbox,25,46,34,43 cru_ts3.21.1901.2012.tmp.dat.nc cru_1901_2012_tmp_TR.nc
## Wed 10 Jul 2013 17:04:15 BST : User ianharris : Program makegridsauto.for called by update.for" ;
## NC_CHAR :source = "Run ID = 1307101324
## Data generated by BADC from:
## tmp.1307101049.dtb" ;
## NC_CHAR :institution = "Data held at British Atmospheric Data Centre, RAL, UK." ;
## NC_CHAR :title = "CRU TS3.21 Mean Temperature" ;
## NC_CHAR :references = "Information on the data is available at http://badc.nerc.ac.uk/data/cru/" ;
## NC_CHAR :comment = "Data restrictions: for academic research use only.Contact BADC for details" ;
## NC_CHAR :contact = "BADC <badc@rl.ac.uk>" ;
## NC_CHAR :CDO = "Climate Data Operators version 1.7.0rc2 (http://mpimet.mpg.de/cdo)" ;
## }
ncdf4_open <- nc_open("data.nc")
class(ncdf4_open)
str(ncdf4_open)
attributes(ncdf4_open)
attributes(ncdf4_open$dim)
attributes(ncdf4_open$var)
attributes(ncdf4_open$var$tmp)
ncdf4_open$var$tmp$longname
ncdf4_open$var$tmp$name
ncdf4_open$var$tmp
ncatt_get(ncdf4_open,'tmp')
ncvar_get(ncdf4_open,"tmp")
ncdf4_tmp <- ncvar_get(ncdf4_open,"tmp")
class(ncdf4_tmp)
dim(ncdf4_tmp)
str(ncdf4_tmp)
attributes(ncdf4_open)
attributes(ncdf4_open$dim)
ncdf4_lon <- ncvar_get(ncdf4_open, ncdf4_open$dim$lon)
ncdf4_lat <- ncvar_get(ncdf4_open, ncdf4_open$dim$lat)
ncdf4_time<- ncvar_get(ncdf4_open, ncdf4_open$dim$time)
rnetcdf_open <- open.nc("data.nc")
class(rnetcdf_open)
str(rnetcdf_open)
rnetcdf_read <- read.nc(rnetcdf_open)
class(rnetcdf_read)
str(rnetcdf_read)
attributes(rnetcdf_read)
var.get.nc(rnetcdf_open,'tmp')
rnetcdf_tmp <- var.get.nc(rnetcdf_open,'tmp')
class(rnetcdf_tmp)
dim(rnetcdf_tmp)
str(rnetcdf_tmp)
attributes(rnetcdf_open)
attributes(rnetcdf_read)
rnetcdf_lon <- var.get.nc(rnetcdf_open, "lon")
rnetcdf_lat <- var.get.nc(rnetcdf_open, "lat")
rnetcdf_time<- var.get.nc(rnetcdf_open, "time")
ncdf4_tmp <- ncvar_get(ncdf4_open,"tmp")
ncdf4_tmp[,,1]
ncdf4_tmp_first<- ncdf4_tmp[,,1]
image(ncdf4_lon, ncdf4_lat, ncdf4_tmp_first)
library("maptools")
turkey_shp <- readShapePoly("turkiye.shp")
image(ncdf4_lon, ncdf4_lat, ncdf4_tmp_first)
plot(turkey_shp, add = T)
library("fields")
image.plot( ncdf4_lon, ncdf4_lat, ncdf4_tmp_first,
xlab = "Lon",
ylab = "Lat",
main = "First Step (01-1901) Temperature for Turkey" )
plot(turkey_shp, add = T)
apply(array1[,,index1],1:2,mean)
image.plot( ncdf4_lon, ncdf4_lat,
apply(ncdf4_tmp[,,1332:1344],1:2,mean) - apply(ncdf4_tmp[,,1:1331],1:2,mean),
xlab = "Lon",
ylab = "Lat",
main = "? for Turkey" )
plot(turkey_shp, add = T)
image.plot( ncdf4_lon, ncdf4_lat,
apply(ncdf4_tmp[,,1332:1344],1:2,mean) - apply(ncdf4_tmp[,,1:1331],1:2,mean),
xlab = "Lon",
ylab = "Lat",
main = " 2012 VS Mean of 1901-2011 for Turkey" )
plot(turkey_shp, add = T)
Comments
How to tell R to ignore a part of your code? (#)